package com.frame.database;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;

import com.frame.database.operate.JBaseOperate;
import com.frame.database.operate.Jsession;
import com.frame.database.operate.OperateManager;

/**
 * @Title: 系统
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company:
 * @author 王海锋
 * @CreatedTime:2012-6-10 上午11:37:58
 * @version 1.0
 */

public class DbOperate extends JBaseOperate{

    private boolean loadLob=false;
    private List<String> batchSqlArr;
    private List<Object[]> batchParmArr;

    public static DbOperate getInstance(){
        return (DbOperate)OperateManager.createJdbc(DbOperate.class);
    }
    public static DbOperate getInstance(String ftg){
        DbOperate dao=(DbOperate)OperateManager.createJdbc(DbOperate.class);
        dao.getSession().setFtg(ftg);
        return dao;
    }
    public List<JdataBean> query(String sql,Object[] parmObj) throws Exception{
        return getSession().query(sql, parmObj);
    }
    public List<JdataBean> query2(String sql,List parmList) throws Exception{
        Object[] obj=null;
        if(parmList!=null){
            obj=new Object[parmList.size()];
            for(int i=0;i<parmList.size();i++)
                obj[i]=parmList.get(i);
        }
        return getSession().query(sql, obj);
    }
    public List query2(String sql,List parmList,Class dataVoclass) throws Exception{
        Object[] obj=null;
        if(parmList!=null){
            obj=new Object[parmList.size()];
            for(int i=0;i<parmList.size();i++)
                obj[i]=parmList.get(i);
        }
        return getSession().query(sql, obj,dataVoclass);
    }
    public List query(String sql,Object[] parmObj,Class dataVoclass) throws Exception{
        return getSession().query(sql, parmObj, dataVoclass);
    }
    public PageResult query(String sql,Object[] parmObj,int size,int pageNo,Class dataVoclass) throws Exception{
        return getSession().query(sql, parmObj, size, pageNo, dataVoclass);
    }
    public PageResult query(String sql,Object[] parmObj,int size,int pageNo) throws Exception{
        return getSession().query(sql, parmObj, size, pageNo);
    }
    public PageResult query2(String sql,List parmList,int size,int pageNo) throws Exception{
        Object[] obj=null;
        if(parmList!=null){
            obj=new Object[parmList.size()];
            for(int i=0;i<parmList.size();i++)
                obj[i]=parmList.get(i);
        }
        return query(sql, obj,size,pageNo);
    }
    public PageResult query2(String sql,List parmList,int size,int pageNo,Class dataVoclass) throws Exception{
        Object[] obj=null;
        if(parmList!=null){
            obj=new Object[parmList.size()];
            for(int i=0;i<parmList.size();i++)
                obj[i]=parmList.get(i);
        }
        return query(sql, obj,size,pageNo,dataVoclass);
    }
    public int insert(String sql,Object[] obj) throws Exception{
        return getSession().update(sql, obj);
    }

    public int insert2(String sql,List parmList) throws Exception{
        Object[] obj=null;
        if(parmList!=null){
            obj=new Object[parmList.size()];
            for(int i=0;i<parmList.size();i++)
                obj[i]=parmList.get(i);
        }
        return getSession().update(sql, obj);
    }
    public void updateBean(TableBean bean) throws Exception{
        getSession().update(bean);
    }
    public int update(String sql,Object[] obj) throws Exception{
        return getSession().update(sql, obj);
    }
    public int update(String tableName,Map updateMap,Map keyMap) throws Exception{
        return getSession().update(tableName, updateMap, keyMap);
    }
    public int update2(String sql,List parmList) throws Exception{
        Object[] obj=null;
        if(parmList!=null){
            obj=new Object[parmList.size()];
            for(int i=0;i<parmList.size();i++)
                obj[i]=parmList.get(i);
        }
        return getSession().update(sql, obj);
    }
    public int delete(String sql,Object[] obj) throws HibernateException, Exception{
        return getSession().delete(sql, obj);
    }
    public int delete2(String sql,List parmList) throws HibernateException, Exception{
        Object[] obj=null;
        if(parmList!=null){
            obj=new Object[parmList.size()];
            for(int i=0;i<parmList.size();i++)
                obj[i]=parmList.get(i);
        }
        return getSession().delete(sql, obj);
    }
    public void addBatch(String sql,Object[] parms){
        if(batchSqlArr==null)batchSqlArr=new ArrayList<String>();
        if(batchParmArr==null)batchParmArr=new ArrayList<Object[]>();
        batchSqlArr.add(sql);
        batchParmArr.add(parms);
    }
    public void execBatch() throws Exception{
        Jsession session=getSession();
        if(batchSqlArr!=null){
            for(int i=0; i<batchSqlArr.size();i++){
                session.update(batchSqlArr.get(i), batchParmArr.get(i));
                session.closeStatements();
            }
            batchSqlArr.clear();batchParmArr.clear();
        }
    }
    public JdataBean getObject(String tbname,Serializable keyValue) throws Exception{
        return getObject(tbname,"id",keyValue);
    }
    public JdataBean getObject(String tbname,String keyCol,Serializable keyValue) throws Exception{
        List<JdataBean> list=DbOperate.getInstance().query("select * from "+tbname+" where "+keyCol+"=?", new Object[]{keyValue});
        if(list.size()>0)
            return list.get(0);
        return null;

    }
    public JdataBean getObject(String sql,Object[] obj) throws Exception{
        List<JdataBean> list=DbOperate.getInstance().query(sql, obj);
        if(list.size()>0)
            return list.get(0);
        return null;
    }

    @Override
    public Jsession getSession() {
        // TODO Auto-generated method stub
        return null;
    }
    public boolean isLoadLob() {
        return loadLob;
    }
    public void setLoadLob(boolean loadLob) {
        getSession().setLoadLob(loadLob);
        this.loadLob = loadLob;
    }

}
